home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group93c.txt
/
000011_icon-group-sender _Sat Jul 10 17:08:09 1993.msg
< prev
next >
Wrap
Internet Message Format
|
1994-02-02
|
3KB
Received: by cheltenham.cs.arizona.edu; Wed, 14 Jul 1993 09:46:07 MST
Date: 10 Jul 93 17:08:09 GMT
From: cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uchinews!ellis!goer@ucbvax.Berkeley.EDU (Richard L. Goerwitz)
Organization: University of Chicago
Subject: string replacement routine
Message-Id: <1993Jul10.170809.17122@midway.uchicago.edu>
Sender: icon-group-request@cs.arizona.edu
To: icon-group@cs.arizona.edu
Status: R
Errors-To: icon-group-errors@cs.arizona.edu
While I'm working here online, I thought I'd post a utility program
I have around that might be of use to some folks.
-Richard
############################################################################
#
# Name: mapstrs.icn
#
# Title: map() for strings
#
# Author: Richard L. Goerwitz
#
# Version: 1.1
#
############################################################################
#
# Mapstrs(s, l1, l2) works like map(), except that instead of taking
# ordered character sequences (strings) as arguments 2 and 3, it
# takes ordered string sequences (lists).
#
# Suppose, for example, you wanted to bowdlerize a string by
# replacing the words "hell" and "shit" with "heck" and "shoot." You
# would call mapstrs as follows:
#
# mapstrs(s, ["hell", "shit"], ["heck", "shoot"])
#
# In order to achieve reasonable speed, mapstrs creates a lot of
# static structures, and uses some extra storage. If you want to
# replace one string with another, it is overkill. Just use the IPL
# replace() routine (in strings.icn).
#
# If l2 is longer than l1, extra members in l2 are ignored. If l1 is
# longer, however, strings in l1 that have no correspondent in l2 are
# simply deleted. Mapstr uses a longest-possible-match approach, so
# that replacing ["hellish", "hell"] with ["heckish", "heck"] will
# work as one would expect.
#
############################################################################
#
# Links: longstr
#
############################################################################
link longstr
procedure mapstrs(s, l1, l2)
local i, s2
static cs, tbl, last_l1, last_l2
if /l1 | *l1 = 0 then return s
if not (last_l1 === l1, last_l2 === l2) then {
cs := ''
every cs ++:= (!l1)[1]
tbl := table()
every i := 1 to *l1 do
insert(tbl, l1[i], (\l2)[i] | "")
}
s2 := ""
s ? {
while s2 ||:= tab(upto(cs)) do
s2 ||:= tbl[tab(longstr(l1))] | move(1)
s2 ||:= tab(0)
}
return s2
end
--
-Richard L. Goerwitz goer%midway@uchicago.bitnet
goer@midway.uchicago.edu rutgers!oddjob!ellis!goer